﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMing.Helper.Net40;


namespace Html5Music.Data
{
    public class DataBase<T> where T : class ,new()
    {
        public DataBase(string path)
        {
            this.FilePath = path;
        }
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        private T _data = null;

        public T Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new T();
                }
                return _data;
            }
            set { _data = value; }
        }

        /// <summary>
        /// 读取
        /// </summary>
        public void Load()
        {
            string data = TextFile.TxtFileRead(this.FilePath);
            if (!string.IsNullOrWhiteSpace(data))
            {
                this.Data = JsonHelper.Deserialize<T>(data);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            string json = JsonHelper.Serialize(this.Data);
            TextFile.TxtFileWrite(this.FilePath, json);
        }
    }
}
